package woa.sort.test20230731;

import java.util.Arrays;

/**
 * @author wangpeng
 * @date 2023/7/31 16:18
 */
public class Sort20230731 {

    public static void bubble(int[] nums) {

        for (int i = 1; i < nums.length; i++) {
            boolean isFinish = true;
            for (int j = 0; j < nums.length - i; j++) {
                if (nums[j] > nums[j + 1]) {
                    int temp = nums[j];
                    nums[j] = nums[j + 1];
                    nums[j + 1] = temp;
                    isFinish = false;
                }
            }
            if (isFinish) {
                return;
            }
        }

    }

    public static void choose(int[] nums) {
        for (int i = 0; i < nums.length - 1; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[j] < nums[i]) {
                    int temp = nums[i];
                    nums[i] = nums[j];
                    nums[j] = temp;
                }
            }
        }
    }

    public static void insert(int[] nums) {
        for (int i = 1; i < nums.length; i++) {
            int temp = nums[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (nums[j] <= temp) {
                    break;
                }
                nums[j + 1] = nums[j];
            }
            nums[j + 1] = temp;
        }
    }

    public static void mergeSort(int[] nums, int l, int r) {
        if (l >= r) {
            return;
        }
        int mid = (r - l) / 2 + l;
        mergeSort(nums, l, mid);
        mergeSort(nums, mid + 1, r);
        merge(nums, l, mid, r);
    }

    private static void merge(int[] nums, int l, int mid, int r) {
        int[] temp = new int[r - l + 1];
        int l1 = l;
        int r1 = mid + 1;
        for (int i = 0; i < temp.length; i++) {
            if (l1 <= mid && r1 <= r && nums[l1] <= nums[r1]) {
                temp[i] = nums[l1];
                l1++;
            } else if (l1 <= mid && r1 > r) {
                temp[i] = nums[l1];
                l1++;
            } else {
                temp[i] = nums[r1];
                r1++;
            }
        }

        for (int i = l; i <= r; i++) {
            nums[i] = temp[i - l];
        }
    }

    public static void quickSort(int[] nums, int l, int r) {
        if (l >= r) {
            return;
        }
        int mid = partition1(nums, l, r);
        quickSort(nums, l, mid - 1);
        quickSort(nums, mid + 1, r);
    }

    private static int partition(int[] nums, int l, int r) {
        int pivot = nums[r];
        int l1 = l, r1 = r - 1;
        while (l1 <= r1) {
            while (l1 <= r1 && nums[l1] < pivot) {
                l1++;
            }
            while (l1 <= r1 && nums[r1] >= pivot) {
                r1--;
            }

            if (l1 < r1) {
                int tmp = nums[l1];
                nums[l1] = nums[r1];
                nums[r1] = tmp;
                l1++;
                r1--;
            }
        }

        int tmp = nums[r];
        nums[r] = nums[l1];
        nums[l1] = tmp;
        return l1;
    }

    private static int partition1(int[] nums, int l, int r) {
        int pivot = nums[r];
        int i = l - 1, j = l;
        for (; j < r; j++) {
            if (nums[j] < pivot) {
                i++;
                swap(nums, i, j);
            }
        }
        swap(nums, i + 1, r);
        return i;
    }

    private static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    public static void main(String[] args) {
        int[] nums = {6,5,1,7,2,4};
        quickSort(nums, 0, nums.length - 1);
        System.out.println(Arrays.toString(nums));
    }
}
